// pages/addressAdd/addressAdd.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: false,
    region: ["", "", ""],
    address: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getModel(options.id);
  },
  getModel: function(id) {
    var that = this;
    httpUtil.getHttp("/user/address/" + id +"/get", {}, function(res) {
      console.log(res.id == null);
      if (res != null && res.id != null && res.id!= "") {
        that.setData({
          address: res,
          flag: true,
          region: [res.province, res.city, res.area]
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      flag: true
    })
  }, addressName:function(e){
    var that=this;
    var value=e.detail.value;
    that.data.address.name = value;
  }, addressPhone: function (e) {
    var that = this;
    var value = e.detail.value;
    that.data.address.phone = value;
  }, addressText: function (e) {
    var that = this;
    var value = e.detail.value;
    that.data.address.text = value;
  }, save: function () {
    var that = this;
    var address = that.data.address;
    if(address.name==''||address.name==null){
      wx.showToast({
        title: '请填写名称',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    } if ((address.phone == ''|| address.phone==null)||address.phone.length>11) {
      wx.showToast({
        title: '请填写电话',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }

    if (that.data.region[0] == '') {
      wx.showToast({
        title: '请选择地址',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
        if (address.text == ''||address.text==null) {
            wx.showToast({
                title: '请填详细地址',
                icon: 'loading',
                duration: 1000,
                mask: true
            })
            return;
        }
    address.province = that.data.region[0];
    address.city = that.data.region[1];
    address.area = that.data.region[2];
    var userInfo = util.getKey("userInfo");
    address.userId = userInfo.id;
    var that = this;
    httpUtil.postHttp("/user/address/save", address, function (res) {
      var pages = getCurrentPages();

      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.getList();
 
      wx.navigateBack()


      
    })
  }
})